<?php
require_once 'lib/BaseModel.php';
class Topics extends Tamtay_Table_Abstract
{
    protected $_name = 'tt_topics';
    protected $_primary = 'tid';

    public function getRefTopics($type, $ref_id, $page=1, $rec_per_page=20, $must_count=false){
      $start = ($page - 1) * $rec_per_page;
      $sql = "SELECT * FROM ".$this->_name." WHERE type=? AND ref_id=? ORDER BY sticky DESC, last_comment_time DESC LIMIT ${start}, ${rec_per_page}";
      
      $db = $this->getSlaveDB();
      $stm = $db->query($sql, array($type, $ref_id));
      $topics = array();
      while($a = $stm->fetch()){
        $topics[] = $a;
      }
      
      $result = array();
      $result['data'] = $topics;
      
      if ($must_count){
        $sql_count = "SELECT count(*) FROM ".$this->_name." WHERE type=? and ref_id=?";
        $c = $db->fetchOne($sql_count, array($type, $ref_id));
        $result['record_count'] = $c;
      }
      
      return $result;
    }
    
    public function getTopic($tid){
      $sql = "SELECT t.*, u.avatar, u.name FROM $this->_name t INNER JOIN users u on u.uid=t.uid WHERE tid=?";
      $db = $this->getSlaveDB();
      return $db->fetchRow($sql, $tid);
    }
    
    public function updateTopic($tid, $c_uid, $c_name, $time){
      $sql = "UPDATE $this->_name SET 
              last_comment_time = ?,
              last_comment_uid = ?,
              last_comment_name = ?,
              comment_count = comment_count + 1
              WHERE tid=?";
      $db = $this->getMasterDB();
      $db->query($sql, array($time, $c_uid, $c_name, (int)$tid));
    }
    
    public function updateViewsCount($tid){
      $sql = "UPDATE $this->_name SET view_count=view_count+1 WHERE tid=?";
      $db = $this->getMasterDB();
      $db->query($sql, $tid);
    }
    
    /*
    public function getTopicsByRank($ref_id, $type, $order_by, $limit){
      $where_clause = "";
      $ref_id = intval($ref_id);
      $limit = intval($limit);
      $type = intval($type);
      if ($ref_id > 0)
        $where_clause = " AND g.gid=$ref_id ";
        
      $qorder_by = "";
      switch($order_by){
        case "newest": 
          $qorder_by = " ORDER BY t.last_comment_time DESC ";
          break;
        case "hottest":
          $qorder_by = " ORDER BY t.comment_count DESC ";
          break;
        default:
          break;
      }
      $sql = "SELECT t.*, g.group_name, g.logo FROM $this->_name t INNER JOIN tt_groups g ON t.ref_id = g.gid
              WHERE t.type=$type".$where_clause.$qorder_by."LIMIT 0,$limit";
      
      $db = $this->getSlaveDB();
      $stm = $db->query($sql);
      $topics = array();
      while($a = $stm->fetch()){
        $topics[] = $a;
      }
      
      return $topics;      
    }
    */
    public function getTopicsByRank($ref_id, $type, $order_by, $limit){
      $where_clause = "";
      $ref_id = intval($ref_id);
      $limit = intval($limit);
      $type = intval($type);
      if ($ref_id > 0){
        switch ($type){
          case FORUM_GROUP_TYPE:
            $where_clause = " AND r.gid=$ref_id ";
            break;
          case FORUM_NETWORK_TYPE:
            $where_clause = " AND r.nwid=$ref_id ";
            break;
          case FORUM_EVENT_TYPE:            
            $where_clause = " AND r.eid=$ref_id ";
            break;
        }
      }
        
      $qorder_by = "";
      switch($order_by){
        case "newest": 
          $qorder_by = " ORDER BY t.last_comment_time DESC ";
          break;
        case "hottest":
          $qorder_by = " ORDER BY t.comment_count DESC ";
          break;
        default:
          break;
      }
      
      switch ($type){
          case FORUM_GROUP_TYPE:
            $sql = "SELECT t.*, r.group_name, r.logo FROM $this->_name t INNER JOIN tt_groups r ON t.ref_id = r.gid
                    WHERE t.type=$type".$where_clause.$qorder_by."LIMIT 0,$limit";
            break;
          case FORUM_NETWORK_TYPE:
            $sql = "SELECT t.*, r.name FROM $this->_name t INNER JOIN mytt_network r ON t.ref_id = r.nwid
                    WHERE t.type=$type".$where_clause.$qorder_by."LIMIT 0,$limit";
            break;
          case FORUM_EVENT_TYPE:            
            $sql = "SELECT t.*, r.event_name, r.logo FROM $this->_name t INNER JOIN tamtay_event r ON t.ref_id = r.eid
                    WHERE t.type=$type".$where_clause.$qorder_by."LIMIT 0,$limit";
            break;
      }
      
      $db = $this->getSlaveDB();
      return $db->fetchAll($sql);
    }
    
    public function getTopicsByCat($cid,$type,$limit=5){
      switch($type){
        case FORUM_EVENT_TYPE:
          $sql = "SELECT t.*, e.eid, e.event_name FROM tamtay_event e 
                  INNER JOIN tt_topics t ON t.type=? AND t.ref_id=e.eid 
                  WHERE e.cid=? ORDER BY t.last_comment_time DESC LIMIT 0, ${limit}";
          break;
        default:
          $sql = "SELECT t.*, g.gid, g.group_name FROM tt_groups g 
                  INNER JOIN tt_topics t ON t.type=? AND t.ref_id=g.gid 
                  WHERE g.group_type=? ORDER BY t.last_comment_time DESC LIMIT 0, ${limit}";
      }
      $db = $this->getSlaveDB();
      return $db->fetchAll($sql,array($type,$cid));
    }
    
    public function getGroupTopicsByCat($type,$cid,$page,$limit=5){
      $start = ($page - 1) * $limit;
      $sql = "SELECT t.*, g.gid, g.group_name FROM tt_groups g 
              INNER JOIN tt_topics t ON t.type=? AND t.ref_id=g.gid 
              WHERE g.group_type=? ORDER BY t.last_comment_time DESC LIMIT ${start}, ${limit}";
      $db = $this->getSlaveDB();
      return $db->fetchAll($sql,array($type,$cid));
    }
    
    public function countGroupTopicByCat($type,$cid){
      $sql = "SELECT count(*) FROM tt_groups g 
              INNER JOIN tt_topics t ON t.type=? AND t.ref_id=g.gid 
              WHERE g.group_type=?";
      $db = $this->getSlaveDB();
      return $db->fetchOne($sql,array($type,$cid));
    }
    
    public function getEventTopicsByCat($type,$cid,$page,$limit=5){
      $start = ($page - 1) * $limit;
      $sql = "SELECT t.*, e.eid, e.event_name FROM tamtay_event e 
              INNER JOIN tt_topics t ON t.type=? AND t.ref_id=e.eid 
              WHERE e.cid=? ORDER BY t.last_comment_time DESC LIMIT ${start}, ${limit}";
      $db = $this->getSlaveDB();
      return $db->fetchAll($sql,array($type,$cid));
    }
    
    public function countEventTopicByCat($type,$cid){
      $sql = "SELECT count(*) FROM tamtay_event e 
              INNER JOIN tt_topics t ON t.type=? AND t.ref_id=e.eid 
              WHERE e.cid=?";
      $db = $this->getSlaveDB();
      return $db->fetchOne($sql,array($type,$cid));
    }
    
    public function insertTopic($data){
      return $this->insert($data);
    }
    
    public function updateTopicByData($tid, $data=array()){
      $this->update($data, "tid=$tid");
//      $sql = "UPDATE $this->_name SET ";
//      if (!is_array($data))
//        return;
//      $db = $this->getMasterDB();
//      foreach ($data as $key=>$value)
//        $sql.=" ".$key."=".$db->quote($value).",";
//      $sql = trim($sql,',');
//      $sql .= " WHERE tid=?";
//      $db->query($sql,(int)$tid);
    }
    
    public function deleteTopic($tid){
      $sql = "DELETE FROM $this->_name WHERE tid=?"  ;
      $db = $this->getMasterDB();
      return $db->query($sql,$tid);
    }
}

